package uebung08_w;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;


public class HoughTransform {
	
	public static int[] normalize(int[] a, int maxvalue){
		int l = a.length, max = 0, min=0;
		for (int i=0;i<l;i++) {
			if (a[i]>max) {
				max = a[i];
			} else if(a[i]<min) {
				min = a[i]; 
			}
		}
//		System.out.println("normalize() :: MAX == "+max+" :: MIN == "+min);
		int[] b = new int[l];
		for (int i=0;i<l;i++) {
			int value = ((a[i]/max)*maxvalue);
			b[i] =  (value << 16 | value << 8 | value) | 0xff000000;
		}
		return b;
	}

	public static int[] getParamspace(int[] lumpixels, int w, int h){
		// rmax, der maximale abstand von 0,0
		int rmax = (int)Math.sqrt(w*w+h*h);
		// akkumulator hat 90grad * rmax
		int[] acc = new int[91*rmax]; // winkel inklusive 0 und 90
		// ueber alle pixel
		for (int i=0;i<lumpixels.length;i++) {
			if (lumpixels[i]==255) {
				int x = i/w, y = i%w;
				int tmpmax = 0, tmpmin = 0;
				for (int phi = 0;phi<91;phi++) { // alle geraden (r,phi) auf denen (x,y) liegt
					int r = (int)(x*Math.cos(phi)+y*Math.sin(phi));
					if (r>tmpmax) tmpmax=r;
					if (r<tmpmin) tmpmin=r;
					if ((r>0)&&(r<=rmax)) acc[r+phi*rmax] += 1; // in parameter space eintragen
//					System.out.println("x="+x+", y="+y+", r="+r+", phi="+phi+", lumpixels["+i+"]="+lumpixels[i]+" :: acc["+(r+phi*rmax)+"]="+acc[r+phi*rmax]);
				}
//				System.out.println("TMPMAX="+tmpmax+", TMPMIN="+tmpmin);
			}
		}
		return acc;
	}

	
	
	
	
	public static void main(String[] args) throws Exception {
		final BufferedImage bim = ImageIO.read(HoughTransform.class.getClassLoader().getResourceAsStream("res/rauschen.png"));
//		BufferedImage bim = ImageIO.read(HoughTransform.class.getClassLoader().getResourceAsStream("res/schienen.png"));
		
		
		int w = bim.getWidth(), h = bim.getHeight(), size=w*h;
		int[] rgbpixels = bim.getRGB(0, 0, w, h, new int[w*h], 0, w);
		int[] lumpixels = tools.ColorConversions.getFastLuminance(rgbpixels, new int[w*h]);
		

		int[] paramspace = getParamspace(lumpixels,w,h);
//		int mmmax = 0;
//		for (int i=0;i<paramspace.length;i++) {
//			int a = paramspace[i];
//			if (mmmax<a)
//				mmmax = a;
//			System.out.println(a+" :: max="+mmmax);
//		}
		
		
		int[] normedparamspace = normalize(paramspace,255);
		int mmax = 0;
		for (int i=0;i<normedparamspace.length;i++) {
			int a = normedparamspace[i];
			if (mmax<a)
				mmax = a;
//			System.out.println(a+" :: max="+mmax);
		}

		
		int rmax = (int)Math.sqrt(w*w+h*h);
		int phi = 91;
		final BufferedImage out = new BufferedImage(rmax, h, BufferedImage.TYPE_INT_ARGB);
		out.setRGB(0, 0, rmax, phi, normedparamspace, 0, rmax);
//		
		
		
		
		
		
		
		
//		JFrame frame = new JFrame("INPUT");
//		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		frame.setBounds(20, 20, bim.getWidth(), bim.getHeight());
//		JComponent comp = new JComponent() {
//			@Override
//			public void paint(Graphics g) {
//				super.paint(g);
//				g.drawImage(bim, 0, 0, this);
//			}
//		};
//		frame.add(comp);
//		frame.setVisible(true);
		
		
		JFrame frameout = new JFrame("OUTPUT");
		frameout.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frameout.setBounds(50, 50, out.getWidth(), out.getHeight());
		JComponent comp2 = new JComponent() {
			@Override
			public void paint(Graphics g) {
				super.paint(g);
				g.drawImage(out, 0, 0, this);
			}
		};
		frameout.add(comp2);
		frameout.setVisible(true);
		
		
	} // END main()
	
	
	
} // END HoughTransform
